Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: add missing before and after callback events for encode response #407

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

bhimrazy
Copy link
Contributor

@bhimrazy bhimrazy commented Jan 9, 2025

What does this PR do?

Adds missing before and after callback events for encode response in the streaming loop

Before submitting
  • Was this discussed/agreed via a Github issue? (no need for typos and docs improvements)
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure to update the docs?
  • Did you write any new necessary tests?

PR review

Anyone in the community is free to review the PR once the tests have passed.
If we didn't discuss your PR in GitHub issues there's a high chance it will not be merged.

Did you have fun?

Make sure you had fun coding 🙃

@bhimrazy bhimrazy changed the title Fix: add missing before and after callback encode response events Fix: add missing before and after callback events for encode response Jan 9, 2025
@@ -70,11 +70,14 @@ def run_streaming_loop(
)
callback_runner.trigger_event(EventTypes.AFTER_PREDICT, lit_api=lit_api)

callback_runner.trigger_event(EventTypes.BEFORE_ENCODE_RESPONSE, lit_api=lit_api)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this one is a bit tricky since y_enc_gen is a generator and the values are actually not materialized till it goes through these lines. so can't add it here.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we can show a warning to users when this callback hook is used with streaming.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, @aniketmaurya.
It seems that even the predict hook does not execute as expected since predict is also a generator. Additionally, it appears that all of the last four events are executed before the predict function runs.

Script I used to test
from litserve import Callback, LitAPI, LitServer


class SimpleStreamAPI(LitAPI):
    def setup(self, device) -> None:
        self.model = lambda x: x

    def decode_request(self, request):
        return request["input"]

    def predict(self, x):
        print(f"Predicting {x}")
        for i in range(4):
            yield self.model(i)

    def encode_response(self, output):
        for out in output:
            yield {"output": out}


class LogCallback(Callback):
    def on_before_predict(self, *args, **kwargs):
        print("Before prediction")

    def on_after_predict(self, *args, **kwargs):
        print("After prediction")

    def on_before_encode_response(self, *args, **kwargs):
        print("Before encoding response")

    def on_after_encode_response(self, *args, **kwargs):
        print("After encoding response")


if __name__ == "__main__":
    api = SimpleStreamAPI()
    server = LitServer(api, stream=True, callbacks=[LogCallback()])
    server.run(port=8000)

One potential approach could be to move the "after event" part to execute after the generator has completed. However, this might not align semantically(in code) with the idea of executing it immediately after the respective functions.

or Alternatively, as you suggested, we could simply display a warning to users when this callback hook is used with streaming.

Looking forward to your thoughts on this 😊.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants